package com.db.stexchem.client.gui;

import javax.swing.table.*;
import javax.swing.*;

import com.db.stexchem.message.Operation;
import com.db.stexchem.message.Order;
import com.db.stexchem.message.OrderType;

import java.awt.*;
import java.awt.event.*;

public class OrderTable extends JTable implements MouseListener {

  public OrderTable(OrderTableModel orderTableModel) {
    super(orderTableModel);
    addMouseListener(this);
  }

  public Component prepareRenderer(TableCellRenderer renderer, int row,
      int column) {
    Order order = (Order) ((OrderTableModel) dataModel).getOrderByRow(row);

    boolean buy = order.getOperation() == Operation.BUY;
    boolean market = order.getOrderType() == OrderType.MARKET;

    DefaultTableCellRenderer r = (DefaultTableCellRenderer) renderer;
    r.setForeground(Color.black);

    if (buy && market)
      r.setBackground(Color.red);
    else if (buy)
      r.setBackground(Color.white);
    else if (!buy && !market)
      r.setBackground(Color.yellow);
    else if (!buy)
      r.setBackground(Color.green);

    return super.prepareRenderer(renderer, row, column);
  }

  public void mouseClicked(MouseEvent e) {
    if (e.getClickCount() != 2)
      return;
    int row = rowAtPoint(e.getPoint());
    Order order = (Order) ((OrderTableModel) dataModel).getOrderByRow(row);
    // application.cancelOrder(order);
  }

  public void mouseEntered(MouseEvent e) {
  }

  public void mouseExited(MouseEvent e) {
  }

  public void mousePressed(MouseEvent e) {
  }

  public void mouseReleased(MouseEvent e) {
  }
}
